/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.validator;

import com.minierp.bean.ClieUserBean;
import com.minierp.dao.ClieUserDao;
import com.minierp.helper.EncCommonHelper;
import com.minierp.model.ClieUser;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

public class ClieAccoValidator implements Validator {
	private ClieUserDao clieUserDao;

	public ClieUserDao getClieUserDao() {
		return this.clieUserDao;
	}

	public void setClieUserDao(ClieUserDao clieUserDao) {
		this.clieUserDao = clieUserDao;
	}

	public boolean supports(Class clazz) {
		return ClieUserBean.class.isAssignableFrom(clazz);
	}

	public void validate(Object obj, Errors errors) {
		ClieUserBean form = (ClieUserBean) obj;
		ClieUser u = form.getUser();
		String decPwd = "";

		try {
			decPwd = EncCommonHelper.decPwd(u.getPassword());
		} catch (Exception arg8) {
			;
		}

		if (u.getUserName() != null && !u.getUserName().trim().equals("")) {
			if (form.getOldPassword() != null
					&& !form.getOldPassword().trim().equals("")) {
				if (form.getCurrentPassword() != null
						&& !form.getCurrentPassword().trim().equals("")) {
					if (form.getConfirmPassword() != null
							&& !form.getConfirmPassword().trim().equals("")) {
						if (form.getOldPassword() != null
								&& form.getOldPassword().equals(decPwd)) {
							if (!form.getConfirmPassword().equals(
									form.getCurrentPassword())) {
								errors.rejectValue("confirmPassword", "",
										"『新密码第1次』『新密码第2次』与不一致");
							} else {
								boolean hasError = false;
								if (!hasError) {
									ClieUser e = this.clieUserDao.get(u
											.getUserName());
									if (u.getId() == 0L) {
										if (e != null) {
											hasError = true;
											errors.rejectValue("user.userName",
													"", "『用户名』已经存在");
										}
									} else if (e != null
											&& e.getId() != u.getId()) {
										hasError = true;
										errors.rejectValue("user.userName", "",
												"『用户名』已经存在");
									}
								}

								if (!hasError) {
									try {
										u.setPassword(EncCommonHelper
												.encPwd(form
														.getCurrentPassword()));
									} catch (Exception arg7) {
										hasError = true;
										errors.rejectValue("currentPassword",
												"",
												"『新密码』有错误：" + arg7.getMessage());
									}
								}
							}
						} else {
							errors.rejectValue("oldPassword", "", "『原密码』不正确");
						}
					} else {
						errors.rejectValue("confirmPassword", "", "请输入『新密码第2次』");
					}
				} else {
					errors.rejectValue("currentPassword", "", "请输入『新密码第1次』");
				}
			} else {
				errors.rejectValue("oldPassword", "", "请输入『原密码』");
			}
		} else {
			errors.rejectValue("user.userName", "", "请输入『用户名』");
		}

	}
}